// coding: utf-8
// @Author : lryself
// @Date : 2020/10/16 21:03
// @Software: GoLand

package main

import (
	"fmt"
	"math"
	"sync"
	"time"
)

var wg sync.WaitGroup

var primes = make(chan int)

// 计算所有小于num的素数的个数，num自定
func main() {
	const num=10000000
	//func1 := countPrime
	//utils.DecoratorExample(&func1, countPrime)
	//func1(num)

	start := time.Now()
	countPrime(num)
	println("用时：",time.Since(start).Microseconds(),"ns")
}


func countPrime(num int){
	for i:=0;i<num;i++{
		wg.Add(1)
		go IsPrime(i)
	}

	go func() {
		wg.Wait()
		close(primes)
	}()
	for {
		prime,ok := <-primes
		if ok {
			fmt.Println(prime)
		} else {
			break
		}
	}
}
func IsPrime(num int){
	defer wg.Done()
	if num<2 {
		return
	}
	temp:=int(math.Sqrt(float64(num)))
	for i:=2;i<temp;i+=1{
		if num%i == 0{
			return
		}
	}
	primes<-num
}